package com.hongkzh.www.other.application;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Handler;

import java.util.HashMap;
import java.util.Map;


/**
 * @author Sandy
 * @ClassName: BaseApplication
 * @Description: 继承了Application，对全局的操作
 * @date 2015-9-22 下午4:02:34
 */
public class BaseApplication extends Application {

    // 获取到主线程的上下文
    private static BaseApplication mContext = null;
    // 获取到主线程的handler
    private static Handler mMainThreadHandler = null;
    // 获取到主线程
    private static Thread mMainThread = null;
    // 获取到主线程的id
    private static int mMainThreadId;
    //Log日志的开关
    private boolean logState = true;
    private Activity app_activity = null;

    @Override
    public void onCreate() {
        super.onCreate();
        /*if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        LeakCanary.install(this);*/
        mContext = this;
        mMainThreadHandler = new Handler();
        //        mMainThread = Thread.currentThread();
        mMainThreadId = android.os.Process.myTid();
    }

    // 对外暴露上下文
    public static BaseApplication getApplication() {
        return mContext;
    }

    // 对外暴露主线程的handler
    public static Handler getMainThreadHandler() {
        return mMainThreadHandler;
    }

    // 对外暴露主线程
   /* public static Thread getMainThread() {
        return mMainThread;
    }*/

    // 对外暴露主线程id
    public static int getMainThreadId() {
        return mMainThreadId;
    }

    public static HashMap<String, Activity> activities = new HashMap<>();

    public static void addActivity(String className, Activity activity) {
        activities.put(className, activity);
    }

    public static void removeActivity(String className) {
        activities.remove(className);
    }

    public static void finishOneActivity(String className) {
        if (activities.get(className) == null)
            return;
        activities.get(className).finish();
        activities.get(className).overridePendingTransition(0, 0);
        activities.remove(className);
    }

    public static void finishAllActivity() {
        //        app_activity = null;


        for (Map.Entry<String, Activity> entry : activities.entrySet()) {
            entry.getValue().finish();
        }
        activities.clear();

    }

    /**
     * 获得当前进程的名字
     *
     * @param context context
     * @return processName
     */
    public static String getCurProcessName(Context context) {

        int pid = android.os.Process.myPid();

        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);

        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
                .getRunningAppProcesses()) {

            if (appProcess.pid == pid) {

                return appProcess.processName;
            }
        }
        return null;
    }

    /**
     * 公开方法，外部可通过 BaseApplication.getCurrentActivity() 获取到当前最上层的activity
     */
    public Activity getCurrentActivity() {
        return app_activity;
    }
}
